using System;
using System.Web.Mvc;
using Microsoft.Web.Mvc;
using SapaFinance.DTO;
using SapaFinance.Services;
using Telerik.Web.Mvc;

namespace SapaFinance.UI.Controllers
{
    public class UserController : Controller
    {
        private readonly IUserService _userService;

        public UserController(IUserService userService)
        {
            _userService = userService;
        }


        public virtual ActionResult Index()
        {
            return View("Index", _userService.GetListDTO());
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Insert(UserDTO dto)
        {
            if (ModelState.IsValid)
            {
                _userService.Insert(dto);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();

        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Delete(Guid id)
        {
            _userService.Delete(id);
            return this.RedirectToAction(x => x.Index());
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Update(UserDTO dto)
        {
            if (ModelState.IsValid)
            {
                _userService.Update(dto);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();
        }
        public virtual ActionResult RenderOwners(Guid id)
        {
            var dtos = _userService.GetSelectedListById(id);
            return View("EditorTemplates/UserList",dtos);
        }
    }
}